package org.sction.action.core;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.sction.security.shiro.ShiroUser;


/**
 * Action 基础类 包含request response session 等常用对象以及公共方法
 * 
 * @author sorc
 * @version 创建时间：2012-3-15 上午8:54:47
 * 
 * @see State
 */
public class BaseAction {

	protected ServletContext servletContext;
	protected ServletRequest servletRequest;
	protected ServletResponse servletResponse;

	protected HttpServletRequest request;
	protected HttpSession session;
	protected HttpServletResponse response;
	protected Map<?, ?> parameterMap;
	protected Map<String, Object> state = new HashMap<String, Object>();

	protected ShiroUser user;

	public void init(ServletContext servletContext,
			ServletRequest servletRequest, ServletResponse servletResponse) {
		this.servletRequest = servletRequest;
		this.servletResponse = servletResponse;
		this.servletContext = servletContext;
		setRequest((HttpServletRequest) servletRequest);
		setResponse((HttpServletResponse) servletResponse);
		session = request.getSession();
		user = ActionUtils.getShiroUser(request);
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
		setParameterMap(request.getParameterMap());
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Map<?, ?> getParameterMap() {
		return parameterMap;
	}
 
	public void outPageDatas() {
		System.out
				.println("-------------------request Parameter---------------------");
		Enumeration<String> rpns = request.getParameterNames();
		while (rpns.hasMoreElements()) {
			String name = rpns.nextElement();
			String value = request.getParameter(name);
			System.out.println(name + ":" + value);
		}
		System.out
				.println("-------------------request Attribute---------------------");
		Enumeration<String> rans = request.getAttributeNames();
		while (rans.hasMoreElements()) {
			String name = rans.nextElement();
			Object value = request.getAttribute(name);
			System.out.println(name + ":" + value);
		}

		System.out.println("-------------------session-----------------------");
		Enumeration<String> sns = session.getAttributeNames();
		while (sns.hasMoreElements()) {
			String name = sns.nextElement();
			Object value = session.getAttribute(name);
			System.out.println(name + ":" + value);
		}
		System.out
				.println("-------------------servletContext InitParameter-----------------------");
		Enumeration<String> scipns = servletContext.getInitParameterNames();
		while (scipns.hasMoreElements()) {
			String name = scipns.nextElement();
			String value = servletContext.getInitParameter(name);
			System.out.println(name + ":" + value);
		}
		// System.out.println("-------------------servletContext Attribute-----------------------");
		// Enumeration<String> scians=servletContext.getAttributeNames();
		// while(scians.hasMoreElements()){
		// String name=scians.nextElement();
		// Object value=servletContext.getAttribute(name);
		// System.out.println(name+":"+value);
		// }

	}

	public Map<?, ?> getParameterSimpleMap() {
		Iterator<?> kit = parameterMap.keySet().iterator();
		Map<String, Object> param = new HashMap<String, Object>();
		while (kit.hasNext()) {
			String key = String.valueOf(kit.next());
			String v[] = (String[]) null;
			try {
				v = (String[]) parameterMap.get(key);
			} catch (Exception e) {
				continue;
			}
			if (v != null && v.length == 1)
				param.put(key, v[0]);
			else
				param.put(key, v);
		}
		return param;
	}

	public void setParameterMap(Map<?, ?> parameterMap) {
		this.parameterMap = parameterMap;
	}

	public void dispose() {
		request = null;
		response = null;
	}

	/**
	 * 快速封装Action执行的状态Map 主要用于转为JSON使用
	 * 
	 * @param state
	 *            {key='state',value=state}
	 * @param code
	 *            {key='code',value=code}
	 * @return
	 */
	public Map<String, Object> pushState(String state, int code) {
		this.state.put("state", state);
		this.state.put("code", code);
		return this.state;
	}

	/**
	 * 快速封装Action执行的状态Map 主要用于转为JSON使用
	 * 
	 * @param state
	 *            {key='state',value=state}
	 * @param code
	 *            {key='code',value=code}
	 * @param data
	 *            {key='data',value=data}
	 * @return
	 */
	public Map<String, Object> pushState(String state, int code, Object data) {
		this.state.put("state", state);
		this.state.put("code", code);
		this.state.put("data", data);
		return this.state;
	}

	/**
	 * 快速封装Action执行的状态Map 主要用于转为JSON使用
	 * 
	 * @param info
	 *            {key='info',value=info}
	 * @param state
	 *            {key='state',value=state}
	 * @param code
	 *            {key='code',value=code}
	 * @return
	 */
	public Map<String, Object> pushState(String info, String state, int code) {
		this.state.put("state", state);
		this.state.put("info", info);
		this.state.put("code", code);
		return this.state;
	}

	/**
	 * 快速封装Action执行的状态Map 主要用于转为JSON使用
	 * 
	 * @param info
	 *            {key='info',value=info}
	 * @param state
	 *            {key='state',value=state}
	 * @param code
	 *            {key='code',value=code}
	 * @param data
	 *            {key='data',value=data}
	 * @return
	 */
	public Map<String, Object> pushState(String info, String state, int code,
			Object data) {
		this.state.put("state", state);
		this.state.put("info", info);
		this.state.put("code", code);
		this.state.put("data", data);
		return this.state;
	}

	/**
	 * 快速封装Action执行的状态Map 主要用于转为JSON使用
	 * 
	 * @param state
	 *            {key='state',value=state.name},{key='code',value=state.value}
	 * @return
	 */
	public Map<String, Object> pushState(State state) {
		this.state.put("state", state.name());
		this.state.put("code", state.value);
		return this.state;
	}

	/**
	 * 快速封装Action执行的状态Map 主要用于转为JSON使用
	 * 
	 * @param state
	 *            {key='state',value=state.name},{key='code',value=state.value}
	 * @param data
	 *            {key='data',value=data}
	 * @return
	 */
	public Map<String, Object> pushState(State state, Object data) {
		this.state.put("state", state.name());
		this.state.put("code", state.value);
		if (data instanceof java.lang.String)
			this.state.put("info", data);
		this.state.put("data", data);
		return this.state;
	}

	/**
	 * 快速封装Action执行的状态Map 主要用于转为JSON使用
	 * 
	 * @param info
	 *            {key='info',value=info}
	 * @param state
	 *            {key='state',value=state.name},{key='code',value=state.value}
	 * @return
	 */
	public Map<String, Object> pushState(String info, State state) {
		this.state.put("state", state.name());
		this.state.put("info", info);
		this.state.put("code", state.value);
		return this.state;
	}

	/**
	 * 快速封装Action执行的状态Map 主要用于转为JSON使用
	 * 
	 * @param info
	 *            {key='info',value=info}
	 * @param state
	 *            {key='state',value=state.name},{key='code',value=state.value}
	 * @param data
	 *            {key='data',value=data}
	 * @return
	 */
	public Map<String, Object> pushState(String info, State state, Object data) {
		this.state.put("state", state.name());
		this.state.put("info", info);
		this.state.put("code", state.value);
		this.state.put("data", data);
		return this.state;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public ServletRequest getServletRequest() {
		return servletRequest;
	}

	public void setServletRequest(ServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public ServletResponse getServletResponse() {
		return servletResponse;
	}

	public void setServletResponse(ServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public ShiroUser getUser() {
		return user;
	}

}
